jsgadget samples
plotly
基本文法
- いつでもデータのキャンバスはplot_ly(data = df)ではじまる。
- x = ~変数 y = ~変数 を引数に指定して、データを割り当てる。
- type引数でグラフのタイプを指定。bar,scatter
- 色分けはgroup(離散のみ),color(連続も可)
追加設定
plot_lyオブジェクトに対して、layout関数を足していく。
layout(設定したい項目 = list(項目をリストでわたす))が文法。
- layout(xaxis = list())
- layout(legend = list)
- layout(title = "")
- layout()
二軸
add_trace()
複数プロット
subplot(p1,p2,nrows = 2)とか。
formattable
formattable(df, list())の形式でリストに列名=関数()でメソッドを渡していく。
- color_tile(“white”,“orange”)
- normalize_bar()
- formatter(“span”, style = x ~ ifelse() )
- style = style(color = “, font.weight =”")
- formatter(“span”, x ~ digits(x,2))
ifelseの色分け
style = x ~ style(color = ifelse(x > 0, “green”, “red”),
font.weight = ifelse(abs(x) > 1, “bold”, NA)))
アイコンテキスト
style = x ~ style(color = ifelse(x > 0, “green”, “red”), font.weight = ifelse(abs(x) > 1, “bold”, NA)), ~ icontext(ifelse(2018 > 2017, “arrow-up”, “arrow-down”), 2018)
style = x ~ icontext(ifelse(x, “ok”, “remove”), ifelse(x, “OK”, “NO”)))
サンプル
#データ用意
df <- mpg %>%
mutate_at(.vars = vars(manufacturer,model,year,cyl,drv,fl,class),
.funs = as.factor)df <- data.frame(
id = 1:10,
name = c("Bob", "Ashley", "James", "David", "Jenny",
"Hans", "Leo", "John", "Emily", "Lee"),
age = c(28, 27, 30, 28, 29, 29, 27, 27, 31, 30),
grade = c("C", "A", "A", "C", "B", "B", "B", "A", "C", "C"),
test1_score = c(8.9, 9.5, 9.6, 8.9, 9.1, 9.3, 9.3, 9.9, 8.5, 8.6),
test2_score = c(9.1, 9.1, 9.2, 9.1, 8.9, 8.5, 9.2, 9.3, 9.1, 8.8),
final_score = c(9, 9.3, 9.4, 9, 9, 8.9, 9.25, 9.6, 8.8, 8.7),
registered = c(TRUE, FALSE, TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE),
stringsAsFactors = FALSE)
formattable(df, list(
age = color_tile("white", "orange"),
grade = formatter("span", style = x ~ ifelse(x == "A",
style(color = "green", font.weight = "bold"), NA)),
area(col = c(test1_score, test2_score)) ~ normalize_bar("pink", 0.2),
final_score = formatter("span",
style = x ~ style(color = ifelse(rank(-x) <= 3, "green", "gray")),
x ~ sprintf("%.2f (rank: %02d)", x, rank(-x))),
registered = formatter("span",
style = x ~ style(color = ifelse(x, "green", "red")),
x ~ icontext(ifelse(x, "ok", "remove"), ifelse(x, "Yes", "No")))
))| id | name | age | grade | test1_score | test2_score | final_score | registered |
|---|---|---|---|---|---|---|---|
| 1 | Bob | 28 | C | 8.9 | 9.1 | 9.00 (rank: 06) | Yes |
| 2 | Ashley | 27 | A | 9.5 | 9.1 | 9.30 (rank: 03) | No |
| 3 | James | 30 | A | 9.6 | 9.2 | 9.40 (rank: 02) | Yes |
| 4 | David | 28 | C | 8.9 | 9.1 | 9.00 (rank: 06) | No |
| 5 | Jenny | 29 | B | 9.1 | 8.9 | 9.00 (rank: 06) | Yes |
| 6 | Hans | 29 | B | 9.3 | 8.5 | 8.90 (rank: 08) | Yes |
| 7 | Leo | 27 | B | 9.3 | 9.2 | 9.25 (rank: 04) | Yes |
| 8 | John | 27 | A | 9.9 | 9.3 | 9.60 (rank: 01) | No |
| 9 | Emily | 31 | C | 8.5 | 9.1 | 8.80 (rank: 09) | No |
| 10 | Lee | 30 | C | 8.6 | 8.8 | 8.70 (rank: 10) | No |
metricsgraphics
基本シンタックスはmjs_xxx、All plots begin with mjs_plot mjs_plot(x=,y=) %>%
- mjs_bar()
- mjs_line() → mjs_add_line()
- mjs_histogram(bar_margin=, )
- mjs_point(color_accessor=, size_accessor=)
プロット以外
- mjs_labs(x=“Weight of Car”, y=“Miles per Gallon”)
- mjs_add_marker()
- mjs_add_baseline()
- mjs_add_legend(legend=c(“X”, “Y”, “Z”))
グリッド
- mjs_grid(p1, p2, ncol=2)
tmp <- data.frame(year=seq(1790, 1970, 10), uspop=as.numeric(uspop))
tmp %>%
mjs_plot(x=year, y=uspop) %>%
mjs_line() %>%
mjs_add_marker(1850, "Something Wonderful") %>%
mjs_add_baseline(150, "Something Awful")tmp %>%
mjs_plot(x=uspop, y=year, width=500, height=400) %>%
mjs_bar() %>%
mjs_axis_x(xax_format = 'plain')dygraphs
time series